package org.walkerljl.commons.inspect;

import java.text.DecimalFormat;

/**
 * Up
 *
 * @author lijunlin
 */
public class Up {

    private static DecimalFormat decimalFormat = new DecimalFormat("0.00");


    /**
     * ??λ???
     */
    public String name;
    /**
     * ????
     */
    public int unit;

    Up(String name, int unit) {
        this.name = name;
        this.unit = unit;
    }

    /**
     * 显示完整的，详细的格式化信息
     *
     * @param ups
     * @param value
     * @return
     */
    public static String format(Up[] ups, long value) {
        StringBuilder values = new StringBuilder();
        for (int i = 0, timeDeepsLength = ups.length; i < timeDeepsLength; i++) {
            Up deep = ups[i];
            if (i == timeDeepsLength - 1) {
                values.insert(0, value + deep.name + " ");
            } else {
                long remainder = value % deep.unit;
                if (remainder > 0) {
                    values.insert(0, remainder + deep.name + " ");
                }
                value = value / deep.unit;
                if (value < 1) {
                    break;
                }
            }
        }
        return values.toString();
    }

    /**
     * 显示最大的格式化信息
     *
     * @param ups
     * @param value
     * @return
     */
    public static String format2(Up[] ups, long value) {
        long max = 1;
        for (int i = 0, timeDeepsLength = ups.length; i < timeDeepsLength; i++) {
            Up deep = ups[i];
            if (i == timeDeepsLength - 1) {
                return decimalFormat.format(value / max) + deep.name;
            } else {
                if (value / deep.unit < max) {
                    return decimalFormat.format((float) value / (float) max) + deep.name;
                }
            }
            max *= deep.unit;
        }
        return null;
    }

}
